import router from '@/router'
import store from '@/store'

// 全局的路由守卫
router.beforeEach(async(to, from, next) => {
  const token = store.state.user.token
  const nextTo = ['/login', '404']
  if (token) {
    // 登录
    if (to.path === '/login') return next('/')
    // 发送请求获取用户数据
    await store.dispatch('user/getUserInfo')
    // 路由进入
    next()
  } else {
    if (nextTo.includes(to.path)) {
      next()
    } else {
      next('/login')
    }
  }
})
